package com.cn.zy;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author zhangyi
 * @date 2019/1/21 16:13
 */
public class SemaphoreTest implements Runnable {
    private static Semaphore semaphore = new Semaphore(5);

    @Override
    public void run() {
        try {
            semaphore.acquire();
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName()+" this is thread is done");
            semaphore.release();
            System.out.println(semaphore.availablePermits());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {

        }
    }

    public static void main(String[] args) {
        ExecutorService executorService =
                Executors.newFixedThreadPool(20);
        for(int i = 0; i <20; i++){
            executorService.submit(new Thread(new SemaphoreTest()));
        }
    }
}
